<?php

namespace App\Admin\Controllers;

use App\Models\Login\GiftCode;
use App\Service\RequestService;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;

class GiftCodeController extends Controller
{
    use ModelForm;
    private static $header;
    public function __construct(Request $request)
    {
        parent::__construct($request);
        self::$header = trans('game.gm_tool');
    }

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header(self::$header);
            $content->description(trans('game.code'));

            $content->body($this->grid());
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header(self::$header);
            $content->description(trans('admin.new'));
            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(GiftCode::class, function (Grid $grid) {
            $grid->model()->orderBy('system', ' desc');
            $grid->code(trans('game.code'));
            $grid->systemName(trans('game.activity_name'));
            $grid->system(trans('game.type'));
            $grid->platform(trans('game.desc'));
            $grid->user_id(trans('game.status'))->display(function ($user_id) {
                return $user_id ? trans('game.receive') : trans('game.not_receive');
            });
            $grid->disableActions();
            $grid->tools(function ($tools) {
                $tools->batch(function ($batch) {
                    $batch->disableDelete();
                });
            });
            $grid->filter(function ($filter) {
                $filter->disableIdFilter();
                $filter->equal('system', trans('game.type'));
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(GiftCode::class, function (Form $form) {
            $form->text('type_name', trans('game.activity_name'))->rules('required|min:3');
            $form->text('type_id', 'ID')->rules('required|numeric');
            $form->text('identifying', trans('game.desc'))->rules('required|min:2');
            $form->slider('number', trans('game.count'))->options(['max' => 2000, 'min' => 1, 'step' => 1, 'postfix' => '个']);
            $form->saving(function ($form) {
                //发送给游戏
                if (!$this->sendGameCode()) {
                    return back()->with(admin_toastr(trans('game.gs_communication_failed'), 'error'))->withInput();
                } else {
                    admin_toastr(trans('admin.succeeded'));

                    return redirect()->route('giftCode.index');
                }
            });
        });
    }

    private function sendGameCode()
    {
        $post = Input::post();
        $option = "14;{$post['number']};{$post['type_id']};{$post['identifying']};{$post['type_name']}";

        return RequestService::sendVip($option, 0);
    }
}
